#include "qtserialport.h"

QtSerialPort::QtSerialPort( QObject *parent)://,QObject * parent1):
        QThread(parent)//,QObject(parent),
{
        fd=-1;
}
int QtSerialPort::open(char *dev)
{
    return fd=open_usb(dev);
}
void  QtSerialPort::run()
{
    char tmp;
    while(1)
    {
        if(read_usb(fd,&tmp,1) > 0)
            emit  readyRecv();
    }

}

QString QtSerialPort::receive()
{
    /*int fd=0;
    int nread;
    char buf[100];
    memset(buf,0,100);
    char dev[20] ="/dev/ttyUSB0\0";
    fd=open_usb(dev);
    int i,j=0;
    cin>>i;*/
    char buf[100];
    char tmp[2];
    int j=0,number=0;

    memset(buf,0,sizeof(buf));
    while((number=read_usb(fd,&tmp,2))>0)
    {
            if(number==-1)
                //return NULL;
            if(tmp=="#|")
                break;
     }
     while((number=read_usb(fd,&tmp,1))>0)
     {
             if(number==-1)
                 //return NULL;
             if(tmp[0]!='\n')
                  buf[j++]=tmp[0];
     }
      return QString(QLatin1String(buf));;
}
int QtSerialPort::send(char *buf)
{
   return  write_usb(fd,buf,strlen(buf));
}
